package com.wz.jiangsu.main.td;

import org.springframework.util.StopWatch;

import java.sql.*;
import java.util.Random;

public class TDengineBatchInsert {

    public static void main(String[] args) {
        // 数据库连接参数
        String url = "jdbc:TAOS-RS://10.25.10.196:6041/device";  // 替换为你的 TDengine 数据库 URL
        String user = "root";  // 数据库用户名
        String password = "taosdata";  // 数据库密码

        // 生成 1000 条模拟数据
        String[][] data = generateTestData(1000);

        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            // 连接到 TDengine 数据库
            conn = DriverManager.getConnection(url, user, password);
//            conn.setAutoCommit(false);  // 禁用自动提交，开启事务

            // 创建批量插入的 SQL 语句
            String sql = "INSERT INTO sensor_1 (ts, device_id, cpu_1m_avg, free_mem, used_mem, net_rssi, net_loss, battery, temperature, humidity, pressure, CO, NO2, PM10) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);

            // 将数据添加到批处理中
            for (String[] row : data) {
                pstmt.setString(1, (row[0]));
                pstmt.setString(2, row[1]);
                pstmt.setFloat(3, Float.parseFloat(row[2]));
                pstmt.setFloat(4, Float.parseFloat(row[3]));
                pstmt.setFloat(5, Float.parseFloat(row[4]));
                pstmt.setFloat(6, Float.parseFloat(row[5]));
                pstmt.setFloat(7, Float.parseFloat(row[6]));
                pstmt.setFloat(8, Float.parseFloat(row[7]));
                pstmt.setFloat(9, Float.parseFloat(row[8]));
                pstmt.setFloat(10, Float.parseFloat(row[9]));
                pstmt.setFloat(11, Float.parseFloat(row[10]));
                pstmt.setFloat(12, Float.parseFloat(row[11]));
                pstmt.setFloat(13, Float.parseFloat(row[12]));
                pstmt.setFloat(14, Float.parseFloat(row[13]));
                pstmt.addBatch();
            }
            StopWatch sw = new StopWatch();
            sw.start("批量插入数据");
            // 执行批量插入操作
            pstmt.executeBatch();
            sw.stop();

            System.out.println("sw.prettyPrint() = " + sw.prettyPrint());

//            conn.commit();  // 提交事务

            System.out.println("Batch insert completed successfully.");

        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null) {
                try {
                    conn.rollback();  // 出现异常时回滚事务
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        } finally {
            // 关闭资源
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        Timestamp timestamp = Timestamp.valueOf("2024-01-01 00:00:00");
//        System.out.println("time = " + timestamp);
//    }

    // 生成模拟数据的方法
    private static String[][] generateTestData(int count) {
        String[][] data = new String[count][14];
        Random rand = new Random();

        for (int i = 0; i < count; i++) {
            data[i][0] = "now()";  // 替换为实际的时间戳
            data[i][1] = "device_" + 1;
            data[i][2] = String.valueOf(rand.nextFloat() * 100);
            data[i][3] = String.valueOf(rand.nextFloat() * 1000);
            data[i][4] = String.valueOf(rand.nextFloat() * 1000);
            data[i][5] = String.valueOf(rand.nextFloat() * 100);
            data[i][6] = String.valueOf(rand.nextFloat() * 100);
            data[i][7] = String.valueOf(rand.nextFloat() * 100);
            data[i][8] = String.valueOf(rand.nextFloat() * 100);
            data[i][9] = String.valueOf(rand.nextFloat() * 100);
            data[i][10] = String.valueOf(rand.nextFloat() * 100);
            data[i][11] = String.valueOf(rand.nextFloat() * 100);
            data[i][12] = String.valueOf(rand.nextFloat() * 100);
            data[i][13] = String.valueOf(rand.nextFloat() * 100);
        }

        return data;
    }
}
